package com.future.lottie.activity;

import android.animation.Animator;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.future.lottie.base.BaseActivity;
import com.future.lottie.databinding.ActivitySettingBinding;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.badge.BadgeUtils;
import com.google.android.material.circularreveal.CircularRevealCompat;
import com.google.android.material.circularreveal.CircularRevealWidget;
import com.google.android.material.datepicker.MaterialDatePicker;

public class SettingActivity extends BaseActivity<ActivitySettingBinding> {


    private static final int REQUEST_CODE_WRITE_SETTINGS = 100;

    @Override
    protected ActivitySettingBinding createViewBinding(LayoutInflater inflater) {
        return ActivitySettingBinding.inflate(inflater);
    }

    @Override
    protected void initView() {

        mViewBinding.button.setOnClickListener(v -> {
            requestWriteSettings();
        });

        mViewBinding.flRoot.setClipChildren(false);
        mViewBinding.flRoot.setClipToPadding(false);
        BadgeDrawable badgeDrawable = BadgeDrawable.create(this);
        badgeDrawable.setNumber(99);
//        mViewBinding.imageView.setClip
        mViewBinding.imageView.getOverlay().add(badgeDrawable);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mViewBinding.imageView.setForeground(badgeDrawable);
        }

        badgeDrawable.updateBadgeCoordinates(mViewBinding.imageView,mViewBinding.flRoot);

        mViewBinding.ivShow.setImageDrawable(mViewBinding.crl.getCircularRevealOverlayDrawable());
        float cx = mViewBinding.crl.getMeasuredWidth()/2f;

        float cy = mViewBinding.crl.getMeasuredHeight()/2f;
        float radius = cx;
        mViewBinding.crl.setOnClickListener(v -> {
//            mViewBinding.crl.setRevealInfo();
//            mViewBinding.crl.setRevealInfo();
//        Animator animator = CircularRevealCompat.createCircularReveal(mViewBinding.crl, cx, cy, radius);
//        animator.setDuration(500);
//        animator.start();

            MaterialDatePicker<Object> objectMaterialDatePicker = new MaterialDatePicker<>();
            objectMaterialDatePicker.show(getSupportFragmentManager(),"date_picker");

        });

    }

    private void requestWriteSettings() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //大于等于23 请求权限
            if (Settings.System.canWrite(getApplicationContext())) {
                return;
            }
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_CODE_WRITE_SETTINGS);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //Settings.System.canWrite方法检测授权结果
                if (Settings.System.canWrite(getApplicationContext())) {
                    showToast("获取了权限");
                } else {
                    showToast("您拒绝了权限");
                }
            }
        }

    }

    private void showToast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }
}